<template>
  <div class="fixed" v-dialogDrag>
    <div class="content el-dialog"
         style="padding:0">
      <div class="msgTop el-dialog__header">
        <span>短信回访</span>
        <i class="el-icon-close fr"
           style="cursor:pointer;line-height:40px;font-size:13px;"
           @click="cancelTransfer"></i>
      </div>
      <el-form :model="ruleForm"
               ref="ruleForm"
               class="demo-ruleForm"
               :rules="rules"
               style="padding:20px 20px 0 20px"
               label-width="100px">
        <el-form-item label="电话号码:"
                      label-width="100px"
                      prop="phone">
          <el-input type="text"
                    v-model="ruleForm.phone"
                    style="width:100%"
                    :autosize="{ minRows: 3}"></el-input>
        </el-form-item>
        <el-form-item label="短信模板:"
                      label-width="100px"
                      prop="templateValue">
          <el-select v-model='ruleForm.templateValue'
                     @change="changeValue"
                     style="width:60%"
                     clearable>
            <el-option v-for='item in descContent'
                       :key='item.id'
                       :label="item.name"
                       :value='item.id'>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="短信内容:"
                      label-width="100px"
                      prop="desc">
          <el-input type="textarea"
                    v-model="ruleForm.desc"
                    style="width:100%"
                    :autosize="{ minRows: 5}"></el-input>
        </el-form-item>
        <el-form-item label-width="10px"
                      class="center">
          <el-button @click="submitForm('ruleForm')"
                     type="button"
                     class="el-button el-button--primary el-button--medium h30Btn ">发送</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { sendMessage } from '@/api/eripDataSynController'
export default {
  props: ['descContent', 'phoneNum', 'isStartNode'],
  data () {
    return {
      ruleForm: {
        phone: this.phoneNum,
        templateValue: '',
        desc: '',
      },
      rules: {
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
        ],
        templateValue: [
          { required: true, message: '请选择短信模板', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入短信内容', trigger: 'blur' }
        ],
      }
    }
  },
  mounted () {
    console.log(this.descContent)
  },
  watch: {
    phoneNum (n, o) {
      this.ruleForm.phone = n
    }
  },
  methods: {
    // 短信模板
    changeValue (val) {
      for (var i = 0; i < this.descContent.length; i++) {
        if (val === this.descContent[i].id) {
          let lebelInfo = this.descContent[i].content
          this.ruleForm.desc = lebelInfo
          return this.ruleForm.desc
        }
      }
    },
    cancelTransfer () {
      this.$emit('noteShow', false)
      this.$refs.ruleForm.resetFields();
      // this.ruleForm.phone = ''
      // this.ruleForm.desc = ''
      // this.ruleForm.templateValue = ''
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = {
            phone: this.ruleForm.phone, // 电话号码
            msgContent: this.ruleForm.desc, // 短信模板内容
            sendName: this.$store.state.userId, // 当前用户处理
            msgTypeName: this.ruleForm.templateValue,
            workOrderId: this.isStartNode ? null : this.$store.state.orderDetailsId + ''//工单编号
          }
          sendMessage(params).then(res => {
            if (res.data.retCode === 200) {
              this.$message({
                type: 'success',
                message: res.data.msg
              })
              this.$emit('noteShow', false)
              this.$refs.ruleForm.resetFields();
              // this.ruleForm.phone = ''
              this.ruleForm.desc = ''
              this.ruleForm.templateValue = ''
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
              // this.ruleForm.phone = ''
              this.ruleForm.desc = ''
              this.ruleForm.templateValue = ''
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.center {
  text-align: center;
  font-size: 16px;
}

.paging-foot {
  padding: 10px 0;
}

</style>
